package com.flyme.module.base.provider.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.flyme.common.core.model.query.CriteriaQuery;
import com.flyme.common.core.model.query.PagerInfo;
import com.flyme.common.core.security.OpenHelper;
import com.flyme.common.core.security.OpenUserAuth;
import com.flyme.common.core.utils.ObjectUtils;
import com.flyme.core.base.supper.entity.EntityMap;
import com.flyme.core.base.supper.service.impl.BaseServiceImpl;
import com.flyme.module.base.client.entity.SysUser;
import com.flyme.module.base.client.entity.SysUsercertificate;
import com.flyme.module.base.provider.mapper.SysUsercertificateMapper;
import com.flyme.module.base.provider.service.SysCertificateService;
import com.flyme.module.base.provider.service.SysUsercertificateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class SysUsercertificateServiceImpl extends BaseServiceImpl<SysUsercertificateMapper, SysUsercertificate> implements SysUsercertificateService{

    @Autowired
    private SysCertificateService certificateService;

    @Override
    public void certificateJob(){
        UpdateWrapper certificate = new UpdateWrapper();
        update(certificate.set(true, "isFull", 0).eq(true, "isFull", 1));
    }

    @Override
    public List<SysUsercertificate> selectByUserId(Long userId) {
        return list(q().eq("userId", userId).orderByDesc("createTime"));
    }

    @Override
    public EntityMap getPeriodrecord(Long usercertificateId) {
        CriteriaQuery<EntityMap> usercertificate=new CriteriaQuery();
        usercertificate.select("u.*,sum(period.period) as period");
        usercertificate.createAlias("period","period");
        usercertificate.eq("id",usercertificateId);
        return  getEntityMap(usercertificate);
    }

    @Override
    public List<EntityMap> getlistCert(Long userId,String type, PagerInfo pagerInfo) {
        CriteriaQuery queryWrapper = new CriteriaQuery<EntityMap>(pagerInfo);
        queryWrapper.addSql("u.id,u.certificateNum,u.validity,u.award,u.isFull,u.createTime as djTime,user.userName,u.awardTime,u.changeStatus as userChange,certificate.applyType," +
            "certificate.id as syscertifiId,certificate.certificateName,certificate.isChange as certifichange,certificate.isConvert,certificate.isStudy");
        queryWrapper.eq("u.userId",userId);
        if (ObjectUtils.isNotEmpty(type) && type.equals("1")) {
            //继续教育
            queryWrapper.eq("certificate.isStudy", "1");
        }
        if (ObjectUtils.isNotEmpty(type) && type.equals("2")) {
            //学分银行
            queryWrapper.eq("certificate.isChange", "1");
        }
        queryWrapper.createAlias("certificate", "certificate");
        queryWrapper.createAlias("user", "user");
        return pageList(queryWrapper).getRows();
    }
    @Override
    public EntityMap getUsercertifie(Long usercertificateId) {
        CriteriaQuery<EntityMap> usercertificate=new CriteriaQuery();
        usercertificate.select("u.*");
        usercertificate.eq("id",usercertificateId);
        return  getEntityMap(usercertificate);
    }

    @Override
    public EntityMap getByIdCard(String idCard, String certificateCode) {
        CriteriaQuery<EntityMap> queryWrapper = new CriteriaQuery();
        queryWrapper.eq("idCard", idCard);
        queryWrapper.eq("certificate.certificateCode", certificateCode);
        queryWrapper.createAlias("certificate", "certificate");
        return getEntityMap(queryWrapper);
    }

    @Override
    public Integer countByIdCard(String idCard, String certificateNum) {
        return count(q().eq("idCard", idCard).eq("certificateNum", certificateNum));
    }

    @Override
    public SysUsercertificate findByIdCard(String idCard, String certificateNum) {
        return getOne(q().eq("idCard", idCard).eq("certificateNum", certificateNum));
    }

    @Override
    public SysUsercertificate getByNum(Long userId, String certificateNum){
        return getOne(q().eq("userId", userId).eq("certificateNum", certificateNum));
    }

    @Override
    public List<EntityMap> getstudyConfig(Long userCeritifiId,PagerInfo pagerInfo) {
        CriteriaQuery queryWrapper = new CriteriaQuery<EntityMap>(pagerInfo);
        queryWrapper.addSql("u.id,u.certificateNum,studyConfig.*");
        queryWrapper.eq("u.id",userCeritifiId);
        queryWrapper.createAlias("certificate", "certificate");
        queryWrapper.createAlias("studyConfig", "studyConfig");
        return pageList(queryWrapper).getRows();
    }

    @Override
    public List<EntityMap> ifexists(Long CeritifiId) {
        OpenUserAuth userAuth = OpenHelper.getUserAuth();
        CriteriaQuery<EntityMap> usercertificate=new CriteriaQuery();
        usercertificate.select("u.*");
        usercertificate.eq("certificateId",CeritifiId);
        usercertificate.eq("userId",userAuth.getUserId());
        return listEntityMaps(usercertificate);
    }

}
